//
//  TPContentVC.swift
//  Auscultation
//
//  Created by zwb on 2021/3/31.
//

import UIKit

class TPContentVC: BaseVC {
    var model:TPListModel?{
        didSet{
            self.navTitle = model?.title

        }
    }
    var dataSource : [TPListModel]?{
        didSet{
            
            table.reloadData()
        }
    }
    
    @IBOutlet weak var table: UITableView!
    
    @IBOutlet weak var topContraint: NSLayoutConstraint!
    override func viewDidLoad() {
        super.viewDidLoad()
        let top_view = UIImageView()
        top_view.image = UIImage(named: "table2")
        top_view.isUserInteractionEnabled = true
        self.view.addSubview(top_view)
        top_view.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(UIDevice.lgf_NavBarH())
        }
        let style = LGFNavigationBarStyle.lgf()
        style.lgf_LeftBtnImageLight = UIImage(named: "darkback")!
        self.lgf_NavigationBar.lgf_line?.isHidden = true
        self.lgf_NavigationBar.lgf_Reload()

        self.topContraint.constant = UIDevice.lgf_NavBarH()
        table.register(UINib.init(nibName: "TPListTableViewCell", bundle: Bundle.main), forCellReuseIdentifier: "TPListTableViewCell")
        table.estimatedRowHeight = 55
        // 自动适应cell高度
        table.rowHeight = UITableView.automaticDimension
        
        table.tableFooterView = UIView()
        table.separatorStyle = .none

    }
    
    
}


extension TPContentVC:UITableViewDelegate,UITableViewDataSource{
    
    // MARK: - UITableViewDataSource
    func numberOfSections(in tableView: UITableView) -> Int {
        guard let arr = self.dataSource else { return 0 }

        return arr.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        guard let arr = self.dataSource else { return 0 }
        let model =  arr[section]
        return model.children.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TPListTableViewCell") as! TPListTableViewCell
        guard let arr = self.dataSource else { return cell }
        let model =  arr[indexPath.section].children[indexPath.row]
        cell.model = model
        return cell

    }
    // MARK: - UITableViewDelegate
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        guard let arr = self.dataSource else { return  }
        
        table.deselectRow(at: indexPath, animated: false)

        let model =  arr[indexPath.section].children[indexPath.row]
        
        if model.ispublic == "1" {
            if model.type == "0" {

                let u3d = U3DVC()
    //            u3d.studyCompeleCourse = {
    //                self.model.eduNum = self.model.eduNum + 1
    //            }
            self.navigationController?.pushViewController(u3d, animated: true)

            }else{
                let examStart = Date().lgf_TimeStampString()
                let vc = WebVC()
                vc.completed = {
                    let examEnd = Date().lgf_TimeStampString()
                    var studyTime = (examEnd.int! - examStart.int!)/1000 / 60
                    studyTime += 1
                    self.saveStudyTime(studyTime: studyTime.string)
                }
                vc.titleString = model.title
                vc.url = EWEUrl.File_URL + model.filePath
                self.navigationController?.pushViewController(vc, animated: true)
            }
        }
//        else{
//            SVProgressHUD.showMessage("敬请期待")
//        }

    }
        
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return UIView()
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.0001
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header = UIView(frame: CGRect(x: 0, y: 0, width: UIDevice.lgf_ScreenW, height: 44))
        let titleL = UILabel(frame: CGRect(x: 10, y: 0, width: UIDevice.lgf_ScreenW, height: 44))
        header.backgroundColor = UIColor.init(lgf_HexString: "F6F6F6")
        header.addSubview(titleL)
        titleL.font = UIFont.boldSystemFont(ofSize: 16)
        titleL.textColor = UIColor.init(lgf_HexString: "0F0F0F")
        guard let arr = self.dataSource else { return header }

        titleL.text = arr[section].title
        return header
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 40
    }

    
    func saveStudyTime(studyTime:String) {
        let paramet = ["studyTime":studyTime]
        
//        guard let userId = EWEUser.lgf.userId else { return  }
        EWERequest.lgf("eduRecord/", paramet, .post,JSONEncoding.default) {  (data) in

        } _: {  (error) in
            SVProgressHUD.showError(error)
        }

    }
}
